<template>
  <div class="cloudbase-status">
    <h2>CloudBase 连接状态</h2>
    
    <div v-if="loading" class="status-loading">
      <div class="spinner"></div>
      <p>正在检查CloudBase连接...</p>
    </div>
    
    <div v-else class="status-result">
      <div v-if="connected" class="status-success">
        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
          <path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"></path>
          <polyline points="22 4 12 14.01 9 11.01"></polyline>
        </svg>
        <p>CloudBase 连接成功！</p>
      </div>
      
      <div v-else class="status-error">
        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
          <circle cx="12" cy="12" r="10"></circle>
          <line x1="15" y1="9" x2="9" y2="15"></line>
          <line x1="9" y1="9" x2="15" y2="15"></line>
        </svg>
        <p>CloudBase 连接失败</p>
        <p class="error-message">{{ errorMessage }}</p>
      </div>
      
      <div class="status-actions">
        <button @click="testConnection" :disabled="loading">
          重新测试连接
        </button>
        <button @click="diagnose" :disabled="loading || connected">
          诊断问题
        </button>
      </div>
      
      <div v-if="diagnosticResults && !connected" class="diagnostic-results">
        <h3>诊断结果</h3>
        <div v-if="diagnosticResults.hasIssues">
          <h4>发现的问题:</h4>
          <ul>
            <li v-for="(issue, index) in diagnosticResults.issues" :key="index">
              {{ issue }}
            </li>
          </ul>
        </div>
        
        <div>
          <h4>建议:</h4>
          <ul>
            <li v-for="(recommendation, index) in diagnosticResults.recommendations" :key="index">
              {{ recommendation }}
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { runConnectionTest, diagnoseConnectionIssues } from '../utils/cloudbaseTest';

export default {
  name: 'CloudBaseStatus',
  data() {
    return {
      loading: true,
      connected: false,
      errorMessage: '',
      diagnosticResults: null
    };
  },
  mounted() {
    this.testConnection();
  },
  methods: {
    async testConnection() {
      this.loading = true;
      this.diagnosticResults = null;
      
      try {
        const result = await runConnectionTest();
        this.connected = result.success;
        if (!result.success) {
          this.errorMessage = result.message;
        }
      } catch (error) {
        this.connected = false;
        this.errorMessage = `测试连接时出错: ${error.message}`;
      } finally {
        this.loading = false;
      }
    },
    async diagnose() {
      this.loading = true;
      
      try {
        this.diagnosticResults = await diagnoseConnectionIssues();
      } catch (error) {
        console.error('诊断过程出错:', error);
      } finally {
        this.loading = false;
      }
    }
  }
};
</script>

<style scoped>
.cloudbase-status {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
  border-radius: 8px;
  background-color: #f9f9f9;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

h2 {
  text-align: center;
  margin-bottom: 20px;
  color: #333;
}

.status-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid rgba(0, 0, 0, 0.1);
  border-radius: 50%;
  border-top-color: #3498db;
  animation: spin 1s ease-in-out infinite;
  margin-bottom: 10px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.status-success, .status-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 20px;
}

.status-success {
  background-color: rgba(46, 204, 113, 0.1);
  color: #27ae60;
}

.status-error {
  background-color: rgba(231, 76, 60, 0.1);
  color: #e74c3c;
}

.status-success svg, .status-error svg {
  margin-bottom: 10px;
}

.error-message {
  font-size: 14px;
  text-align: center;
  margin-top: 10px;
}

.status-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 20px;
}

button {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  background-color: #3498db;
  color: white;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.3s;
}

button:hover {
  background-color: #2980b9;
}

button:disabled {
  background-color: #95a5a6;
  cursor: not-allowed;
}

.diagnostic-results {
  margin-top: 20px;
  padding: 15px;
  border-radius: 8px;
  background-color: #f5f5f5;
}

.diagnostic-results h3 {
  margin-top: 0;
  color: #333;
}

.diagnostic-results h4 {
  margin-bottom: 10px;
  color: #555;
}

.diagnostic-results ul {
  padding-left: 20px;
}

.diagnostic-results li {
  margin-bottom: 5px;
}
</style>